import NoviceGuide from "../../UI/NoviceGuide";
import DestructibleFacility from "../../facility/DestructibleFacility";
import { Location, MapLength } from "../../info/Predefine";
import GameManager from "../../manager/GameManager";
import PlayerManager from "../../manager/PlayerManager";
import DestructibleFlyer from "../DestructibleFlyer";

const { ccclass, property } = cc._decorator;
/**
 * 侦察机
 */
@ccclass
export default class Scout extends DestructibleFlyer {

    @property([cc.SpriteFrame])
    scoutColor: cc.SpriteFrame[] = [];
    target: Location;

    init(base: DestructibleFacility) {
        super.init(base);
        cc.find("body/color", this.node).getComponent(cc.Sprite).spriteFrame = this.scoutColor[base.player.playerId];
    }

    dispatch(target: Location) {
        super.dispatch(target);
        let targetPosition = GameManager.instance.getGamePosition(target);
        this.getComponent(cc.Animation).play("scoutFly");
        this.target = target;
        this.moveComp.setTarget(targetPosition, this.onArrive.bind(this));
        this.dispatchSuccess(0.8);
    }

    onArrive(): void {
        //到达后 上下左右四个点显示
        let viewRadis = this.flyerInfo.爆炸后获得视野半径 + this.player.extraView;;
        let leftUp = { locationX: Math.max(0, this.target.locationX - viewRadis), locationY: Math.max(0, this.target.locationY - viewRadis) };
        let rightDown = { locationX: Math.min(MapLength - viewRadis, this.target.locationX + viewRadis), locationY: Math.min(MapLength - viewRadis, this.target.locationY + viewRadis) };
        let tempLocations = [];
        for (let i = leftUp.locationX; i <= rightDown.locationX; i++) {
            for (let j = leftUp.locationY; j <= rightDown.locationY; j++) {
                let location = { locationX: i, locationY: j };
                this.player.getLocationView(location);
                tempLocations.push(location);
            }
        }

        if (this.player == PlayerManager.instance.controllerPlayer && tempLocations.find(item => GameManager.instance.getFacilityByLocation(item))) {
            this.node.emit(NoviceGuide.EventType.侦察到敌人的建筑);
        }
        super.onArrive();
    }
}
